package com.setup.trac.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.taglibs.standard.lang.jstl.test.StaticFunctionTests;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import template.pojo.Stati;

import com.setup.trac.dao.DAOService;
import com.setup.trac.pojo.Evento;
import com.setup.trac.pojo.Tickets;
import com.setup.trac.pojo.wrappers.UtentiView;
import com.setup.trac.service.ManageTicketService;
import com.setup.trac.service.SearchService;
import com.setup.trac.util.SQLCondition;
import com.setup.trac.util.StatoConstant;

public class CancellaTicketController extends AbstractController {

	
	private SearchService searchService;
	private DAOService daoService;
	private ManageTicketService manageTicketService;
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		SQLCondition sqlCondition = new TicketFinderByIdTicket(request.getParameter("idTicket"));
		Tickets t = getSearchService().find(new Tickets[0], sqlCondition, Tickets.class)[0];
		t.setStati(StatoConstant.ANNULLATO);
		UtentiView uw = (UtentiView) request.getSession(false).getAttribute("loginForm");
		t.setUtentiByIdUtenteUltimaModifica(uw.getUtenti());
		t.setDataUltimaModifica(new Date());
		getDaoService().update(t);
		Evento evento = new Evento();
		evento.setDataCreazione(new Date());
		evento.setInviato("0");
		evento.setTickets(t);
		evento.setTipoEvento("INFO");
		manageTicketService.insertEventoDaInviare(evento);

		response.getOutputStream().write("true".getBytes());
		return null;
	}
	public SearchService getSearchService() {
		return searchService;
	}
	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}
	public DAOService getDaoService() {
		return daoService;
	}
	public void setDaoService(DAOService daoService) {
		this.daoService = daoService;
	}
	public ManageTicketService getManageTicketService() {
		return manageTicketService;
	}
	public void setManageTicketService(ManageTicketService manageTicketService) {
		this.manageTicketService = manageTicketService;
	}

}
